//
//  CZVisitorView.swift
//  git
//
//  Created by 徐 鹏超 on 16/4/8.
//  Copyright © 2016年 徐 鹏飞. All rights reserved.
//

import UIKit
import SnapKit
/*
代理的实现步骤:
1. 声明一个协议,定义代理方法 -> NSObjectProtocol
2. 设置一个代理对象 -> weak (weak var delegate: Delegate?)
3. 调用代理方法  -> delegate?.
4. 实现代理对象,,一定要遵守协议 ->
5. 实现代理方法
*/

// 一定要继承自 NSObjectProtocol,如果不继承自NSObjectProtocol , delegate无法使用weak修饰
protocol CZVisitorViewDelegate:NSObjectProtocol{
    // 注册的代理方法
    func didRegisterMethod()
    // 登录的代理方法
    func didLoginMethod()

}




class CZVisitorView: UIView {
    // 设置一个代理对象
    weak var delegate:CZVisitorViewDelegate?
    
    //MARK:--按钮点击方法
    func registerMethod(){
        
        
        print("zhuce")
        // 跳转到注册页面
        delegate?.didRegisterMethod()
    }
    
    func loginMethod(){
        print("denglu")
        delegate?.didLoginMethod()
    }

    
    // 对外提供一个方法,来实现内部属性的更改
    //
    func setupInfo(image: String?,message: String){
        
        // image 为空的时候 是首页
        // iamge 不为空,是其他三个页面
        
        // if let 用于判断可选项 是否有值, 如果有值,执行括号里的方法
        if let image = image {
            
            iconImageView.image = UIImage(named: image)
            
            // 隐藏首页动画
            homeImageView.hidden = true
            
            noticeLabel.text = message
            
        } else {
            
            homeImageView.hidden = false
            
            //调用核心动画
            startAnimation()
        }
    }
    

    override init(frame : CGRect){
        
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        addSubview(homeImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
    
        addSubview(noticeLabel)
        addSubview(registerButton)
        addSubview(loginButton)
    
    
    
        // 设置约束
        // 可以先随便设置frame,然后再调节
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-10)
        }
    
         // iconImageView 水平居中,垂直居中
        iconImageView.snp_makeConstraints { (make) -> Void in
            //
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
            
    }
        homeImageView.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY)
    }
    // 提示label -- 水平居中/距离上边(homeImageView)有间距/宽度
        noticeLabel.snp_makeConstraints { (make) -> Void in
            //
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(homeImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
    }
    // 注册按钮的约束 -- 左边->noticeLable的左边对其;上边->noticeLabel的底部对其,有一个间距;宽(100);高(30); 4个约束可以满足约束条件
    registerButton.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(noticeLabel.snp_left)
        make.top.equalTo(noticeLabel.snp_bottom).offset(10)
        
        make.width.equalTo(100)
        make.height.equalTo(30)
    }
    
    
    // 登录按钮的约束 -- 右边->noticeLable的右边对其;上边->noticeLabel的底部对其,有一个间距;宽(100);高(30); 4个约束可以满足约束条件
    loginButton.snp_makeConstraints { (make) -> Void in
        // 一定是 snp_right
        make.right.equalTo(noticeLabel.snp_right)
        make.top.equalTo(noticeLabel.snp_bottom).offset(10)
        
        make.width.equalTo(100)
        make.height.equalTo(30)
    }
    
    }
    
    
    
    func startAnimation(){
        let baseAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        baseAnimation.duration = 10
        
        baseAnimation.toValue = M_PI
        
        baseAnimation.repeatCount = MAXFLOAT
        // 当我们切换页面的时候,动画会停止
        baseAnimation.removedOnCompletion = false
        
        homeImageView.layer.addAnimation(baseAnimation, forKey: nil)    
    }
    
    ///添加一个遮罩视图
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()

    
    /// 4个控制器共有的图片
   private lazy var iconImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
        
    }()
    ///首页的转动图片
    private lazy var homeImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
        
    }()
    /// 提示语Label
    private lazy var noticeLabel:UILabel = {
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(16)
        
        return label
    }()
    /// 注册按钮
    private lazy var registerButton: UIButton = {
       let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "registerMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    ///登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "loginMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    
    

}
